package com.anzym.android.kegdroidkey;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.anzym.android.kegdroidlibrary.database.BeerDataSource;
import com.anzym.android.kegdroidlibrary.models.Beer;
import com.anzym.android.kegdroidlibrary.models.BeerOrder;
import com.anzym.android.kegdroidlibrary.models.KegDroid;

import java.util.ArrayList;

public class BeerOrderDialog extends Dialog {
    
    private static final String TAG = BeerOrderDialog.class.getSimpleName();
    
    KegDroidKeyApplication keyApp;
    KegDroidKeyActivity kyA;
    Beer[] beers;
    //BeerOrder beerOrder;
    KegDroid kd;
    
    TextView beer0Text;
    TextView beer1Text;
    ImageView beer0Image;
    ImageView beer1Image;
    
    ListView beerListView;
    ArrayList<Beer> beerArrayList;
    
    public VolumeSelectionDialog vsDialog;

    public BeerOrderDialog(Context context, KegDroid k) {
        super(context);
        this.kyA = (KegDroidKeyActivity) context;
        this.kd = k;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_beer_order);
        keyApp = KegDroidKeyApplication.getInstance();
        beerArrayList = new ArrayList<Beer>();
        beerArrayList.add(getBeer(kd.getBeer_id(0)));
        beerArrayList.add(getBeer(kd.getBeer_id(1)));

        beerListView = (ListView)  findViewById(R.id.beer_list_container);
        beerListView.setAdapter(new BeerListAdapter(this.kyA, beerArrayList));
        beerListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v,
                    int position, long id) {

               Log.d(TAG, "Selected position: " + position);
               Log.d(TAG,"AndroidID: " + kd.getAndroid_id());
               keyApp.getBeerOrder().setAndroidId(kd.getAndroid_id());
               keyApp.getBeerOrder().setTapNumber(position);
               

               Beer b = new Beer();
               b = beerArrayList.get(position);
               Log.d(TAG, "beer: " + b.getName());
               keyApp.getBeerOrder().setBeerId(b.getId());

               vsDialog = new VolumeSelectionDialog(kyA, b, BeerOrderDialog.this);
               vsDialog.show();
               
            }
        });

    }
    

    public Beer getBeer(long id) {
        Beer b;
        BeerDataSource beerDS = new BeerDataSource(kyA);
        beerDS.open();
        b = beerDS.getBeer(id);
        beerDS.close();
        return b;
        
    }

}
